数据分析 |
您所在的位置:网站首页 › r语言 summarize › 数据分析 |
备忘录地址(用来查询一些参数)
https://www.maths.usyd.edu.au/u/UG/SM/STAT3022/r/current/Misc/data-visualization-2.1.pdf R语言软件参数 R3.6.0 安装包、导入包 install.package('ggplot') library(ggplot) 首先加载数据集这里用的是示例数据,R自带的一个包提供的数据集。 dslabs包是R中的一个数据科学工具包,它提供了一系列数据集和函数,用于帮助研究人员探索新的数据科学概念,进行数据分析和机器学习模型构建。 library(dslabs) data(murders) 定义ggplot对象 ggplot初始化图形管道符(|>, %>%, %>%, %$% 和%%),|>是R4.1版本开始的自带的管道符,将左侧的输出结果传递给右侧命令,因为这里用的是3.6版本的所以就用%>%管道符,测试发现|>不能被识别。 ggplot(data = murders)##或者murders |> ggplot()也可以把ggplot图赋值给一个对象p p % ggplot() + LAYER 1 + LAYER 2 + … + LAYER N murders %>% ggplot()+ geom_point(aes(x = population/10^6, y = total))#添加散点图,添加x轴population,y轴total几何函数名称遵循以下模式:geom_X其中 X 是几何的名称,这里散点图是point,要用其他图的话可以去备忘录上的geoms上看。 方式二,在定义的对象p上叠加图层 p + geom_point(aes(x = population/10^6, y = total))#p是前面定义过的含有数据的初始图 继续添加图层在散点图基础上添加一个文本组件 p + geom_point(aes(population/10^6, total)) + geom_text(aes(population/10^6, total, label = abb))#Label = abb 指的是标签 (label) 的缩写可以看到文本向右移动了 ,点也变大了 先查看下ggplot参数 args(ggplot)然后就可以直接使用了,不用在每个组件中定义x轴,y轴 p + geom_point(size = 3) + geom_text(nudge_x = 1.5)得到 想要单独让某个组件有自己的坐标 p + geom_point(size = 3) + geom_text(aes(x = 10, y = 800, label = "Hello there!"))
scale_x_continuous函数可以用来在指定的坐标轴上对数据进行缩放。 p + geom_point(size = 3) + geom_text(nudge_x = 0.05) + scale_x_continuous(trans = "log10") + scale_y_continuous(trans = "log10") #用于将坐标轴的连续数据变换为以10为底的对数尺度使用xlab,ylab和ggtitle p + geom_point(size = 3) + geom_text(nudge_x = 0.05) + scale_x_log10() + scale_y_log10() + xlab("Populations in millions (log scale)") + ylab("Total number of murders (log scale)") + ggtitle("US Gun Murders in 2010")
这里代表全国的平均谋杀率 y=rx geom_abline是画线的,log10是因为之前坐标轴是转换到log10标准的 geom_abline(intercept, slope, linetype, color, size) 参数说明:intercept:y轴的截距 slope:斜率linetype:线条样式color:线条颜色size:线条宽度 pull()函数是R语言中用于提取对象的函数。它的作用是从已有的数据框、向量或列表中提取特定行或列的数据。 #计算斜率 r % summarize(rate = sum(total) / sum(population) * 10^6) %>% pull(rate) p % pull(rate) murders %>% ggplot(aes(population/10^6, total, label = abb)) + geom_abline(intercept = log10(r), lty = 2, color = "darkgrey") + geom_point(aes(col=region), size = 3) + geom_text_repel() + scale_x_log10() + scale_y_log10() + xlab("Populations in millions (log scale)") + ylab("Total number of murders (log scale)") + ggtitle("US Gun Murders in 2010") + scale_color_discrete(name = "Region") + theme_economist() 描述分布 条形图 murders %>% ggplot(aes(region)) + geom_bar()
用的是范例数据里的heights的数据,geom_histogram()函数,添加了标题,x轴标签 library(dslabs) data(heights) x % filter(sex == "Female") %>% ggplot(aes(height)) + geom_histogram(binwidth = 1, fill = "blue", col = "black") + xlab("Female heights in inches") + ggtitle("Histogram")adjust参数控制密度图的平滑度,取值范围为0~1,默认值为1,值越大,则平滑度越高。 heights %>% filter(sex == "Female") %>% ggplot(aes(height)) + geom_density(fill="blue",adjust = 2)快速绘制一个向量值的直方图、两个向量值的散点图或使用分类向量和数值向量的箱线图 qplot(x, y, data, geom, color, fill, size, shape, alpha, etc.) 其中,x和y参数用于指定绘图变量,data参数指定数据集,geom参数用于指定绘图类型,color、fill、size、shape、alpha等参数则可用于控制图表的外观。 两个向量的值绘制散点图 data(murders) x % qplot(sex, height,data=.)
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |